package com.miaoxun.service.impl;


import com.basemapper.BasicMapper;
import com.miaoxun.dao.SysMapper;
import com.miaoxun.domain.model.DataModel;
import com.miaoxun.domain.model.SysModel;
import com.miaoxun.domain.param.BaseParam;
import com.miaoxun.domain.util.PageUtil;
import com.miaoxun.service.util.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

@Service
public class SysService extends BaseServiceImpl<String>   {
    @Override
    protected BasicMapper getMapper() {
        return null;
    }

    @Autowired
    SysMapper mapper;

    public DataModel<List<SysModel>> GetTables(BaseParam p) {
        com.github.pagehelper.Page<Object> page = PageUtil.startPage(p.getOffset(), p.getLimit(), p.getOrder(), p.getSort());
        List<SysModel> syss = mapper.list();
        DataModel<List<SysModel>> dataModel = new DataModel();
        dataModel.setCount(page.getTotal());
        dataModel.setList(syss);
        return dataModel;
    }


    public  List<SysModel> list() {
        //List<Map<String, Object>> list = mapper.list();
        return  mapper.list();
    }


    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = mapper.get(tableName);
            //查询列信息
            List<Map<String, String>> columns = mapper.listColumns(tableName);
            //生成代码
            GenUtils.generatorCode(table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
}
